package liuhao.base;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class Ver {

	/**
	 * 设置宽
	 */
	private int width=70;
	/**
	 * 设置高
	 */
	private int height=35;
	/**
	 * 定义一个字体名字的数组
	 */
	private String[] fontNames={"宋体","黑体","楷体","隶书","幼圆"};
	/**
	 * 定义一个字符串
	 */
	private String chars = "123456789abcdefghjklmnpqrskuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
	/**
	 * 定义黑板的背景颜色
	 */
	private Color color = new Color(255,255,255);
	/**
	 * 定义一个存储验证码的字符串
	 */
	private String text;
	/**
	 * 定义一个随机数
	 */
	private Random ran = new Random();
	
	
	/**
	 * 定义一个获取随机颜色的方法
	 * @return
	 */
	
	public Color ranColor(){
		int r = ran.nextInt(255);
		int g = ran.nextInt(255);
		int b = ran.nextInt(255);
		Color color = new Color(r,g,b);
		return color;
	}
	/**
	 * 定义一个获取随机字体的方法
	 */
	public Font ranFont(){
		int index = ran.nextInt(fontNames.length);
		String fontName = fontNames[index];
		//0表示无样式、1表示粗体、2表示斜体、3表示粗斜体
		int style = ran.nextInt(4);
		
		int fontSize = ran.nextInt(4)+24;
		
		Font font = new Font(fontName,style,fontSize);
		return font;
	}
	/**
	 * 定义一个得到随机字符的方法
	 */
	public String ranChar(){
		int index = ran.nextInt(chars.length());
		char c = chars.charAt(index);
		String ranStr = String.valueOf(c);
		return ranStr;
	}
	
	/**
	 * 定义一个随机字符的方法
	 */
	public String ranChar(Random ran){
		int index = ran.nextInt(chars.length());
		char c = chars.charAt(index);
		String ranStr = String.valueOf(c);
		return ranStr;
	}
	/**
	 * 画干扰线
	 */
	public void drawLine(BufferedImage bi){
		int num = 4;
		Graphics2D g = (Graphics2D)bi.getGraphics();
		for(int i=0;i<num;i++){
			g.setColor(ranColor());
			g.setStroke(new BasicStroke(1.5f));
			int x1 = ran.nextInt(width);
			int y1 = ran.nextInt(height);
			int x2 = ran.nextInt(width);
			int y2 = ran.nextInt(height);
			g.drawLine(x1, y1, x2, y2);
		}
	}
	/**
	 * 画圆点
	 */
	public void drawOval(BufferedImage bi){
		int num =50;
		Graphics2D g = (Graphics2D) bi.getGraphics();
		for(int i=0;i<num;i++){
			g.setColor(ranColor());
			int x = ran.nextInt(width);
			int y = ran.nextInt(height);
			g.drawOval(x, y, 1, 1);
		}
	}
	/**
	 * 定义一个得到图片缓冲区的方法
	 */
	public BufferedImage createImage(){
		BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
		Graphics2D g = (Graphics2D) bi.getGraphics();
		g.setColor(this.color);
		g.fillRect(0, 0, width, height);
		return bi;
	}
	
	/**
	 * 画图
	 */
	public BufferedImage getImage(){
		BufferedImage bi = createImage();
		Graphics2D g = (Graphics2D) bi.getGraphics();
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<4;i++){
			while(true){
				if(i==0){
					Random random = new Random();
					String ranStr = ranChar(random);
					if(ranStr.matches("^\\d|^\\p{Upper}")){
						sb.append(ranStr);
						int w = i*(width/4);
						g.setColor(ranColor());
						g.setFont(ranFont());
						g.drawString(ranStr,w,height-5);
						break;
					}
				}else{
					String ranStr = ranChar();
					sb.append(ranStr);
					int w = i*(width/4);
					g.setColor(ranColor());
					g.setFont(ranFont());
					g.drawString(ranStr,w,height-5);
					break;
				}
				
			}
			
				
			
			
		}
		this.text = sb.toString();
		//this.drawLine(bi);
		this.drawOval(bi);
		return bi;
	}
	
	public String getText(){
		return text;
	}
	public void outPut(BufferedImage bi,OutputStream out) throws IOException{
		ImageIO.write(bi, "jpeg", out);
	}
}
